/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 *
 */
import path from 'path';

import {expect} from '@jest/globals';
import fse from 'fs-extra';
import JSON5 from 'json5';

import {initExecuteCommand, runHvigorFailAsync} from '../../common/common';
import {buildAppAndHapsAndCheckFile, buildServiceCard, readJson5} from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
  SignType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { TestCaseFun } from '../../plugins/task-builder/test-case';

const projectBuilderOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
};
const taskConfig = {
  caseLevel: CaseLevel.L1,
  sign: SignType.NoSign,
  ohpmInstall: IsEnable.DisAble,
};
const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const entryPath = project.getModule('entry').moduleRootPath();
  const entryInsightintentsPath = path.resolve(
    entryPath,
    'build/default/cache/default/default@CompileArkTS/esmodule/debug/entry/src/main/ets/insightintents/IntentExecutorImpl.protoBin',
  );
  const entryInsightintentJsonPath = path.resolve(
    entryPath,
    'build',
    'default',
    'intermediates',
    'res',
    'default',
    'resources',
    'base',
    'profile',
    'insight_intent.json',
  );
  await buildServiceCard(entryPath);
  const insightIntentJsonPath = path.resolve(entryPath, 'src', 'main', 'resources', 'base', 'profile', 'insight_intent.json');
  // 仅勾选PlayMusic
  fse.cpSync(
    path.resolve('test', 'resources', 'insightintents', 'hasMusicNoMscList', 'IntentExecutorImpl.ets'),
    path.resolve(entryPath, 'src', 'main', 'ets', 'insightintents', 'IntentExecutorImpl.ets')
  );
  fse.cpSync(
    path.resolve('test', 'resources', 'insightintents', 'hasMusicNoMscList', 'insight_intent.json'),
    insightIntentJsonPath
  );
  await buildAppAndHapsAndCheckFile(project, taskConfig, record);
  expect(await fse.pathExists(entryInsightintentsPath)).toBe(true);
  expect(await fse.pathExists(entryInsightintentJsonPath)).toBe(true);

  const insightIntentJson = await readJson5(insightIntentJsonPath);
  insightIntentJson.insightIntents[0].form = {
    'formName': 'widget123',
    'ability': 'EntryFormAbility'
  };
  const commands = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: 'assembleHap'
  });
  await fse.writeFile(insightIntentJsonPath, JSON5.stringify(insightIntentJson, null, 2), 'utf-8');
  const result1 = await runHvigorFailAsync(project.projectRootPath(), commands);
  record.addStep('修改insight_intent.json文件中的form/formName后构建', result1);
  expect(result1.stderr).toContain('BUILD FAILED');
  expect(result1.stderr).toContain('Invalid form name \'widget123\'.');
  expect(result1.stderr).toContain('Make sure the name has been configured in the form_config.json file.');
};
TestCaseBuilder.builder()
  .caseName('DTS2024091914430')
  .description('添加StockFundsDomain的意图框架，InsightIntent.json文件中form/formName的值改成错误的，构建报错')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testFun)
  .load();
